﻿using Modelado.Modulos.ClasesModelado;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
    public class AD_EsqPlanta
    {
        public void CRE_EsqPlanta(SqlCommand comando, MOD_EsqPlanta obj)
        {
            try
            {
                string sp = "SP_CRE_ESQPLANTA";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ESQ_NOMBREESQPLANTA", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.ESQ_nombreesqplanta;

                comando.ExecuteNonQuery();
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public void DEL_EsqPlanta(SqlCommand comando, int codigo)
        {
            try
            {
                string sp = "SP_DEL_EsqPlanta";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ESQ_CODESQPLANTA", SqlDbType.Int));

                comando.Parameters[0].Value = codigo;

                comando.ExecuteNonQuery();

            }
            catch (Exception exc)
            {

                throw exc;
            }
        }


        public void UPD_EsqPlanta(SqlCommand comando, MOD_EsqPlanta obj)
        {
            try
            {
                string sp = "SP_UPD_EsqPlanta";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ESQ_CODEESQPLANTA", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ESQ_NOMBREESQPLANTA", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.ESQ_codesqplanta;
                comando.Parameters[1].Value = obj.ESQ_nombreesqplanta;

                comando.ExecuteNonQuery();
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }


        public List<MOD_EsqPlanta> SEL_Grid_EsqPlanta(SqlCommand comando)
        {
            try
            {
                string sp = "SP_SEL_Grid_EsqPlanta";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_EsqPlanta> lista = new List<MOD_EsqPlanta>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_EsqPlanta obj = new MOD_EsqPlanta();
                    obj.ESQ_codesqplanta = Convert.ToInt32(dt.Rows[i]["ESQ_CODESQPLANTA"]);
                    obj.ESQ_nombreesqplanta = dt.Rows[i]["ESQ_NOMBREESQPLANTA"].ToString();

                    lista.Add(obj);
                }

                return lista;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public List<MOD_EsqPlanta> SEL_ESQPLANTA(SqlCommand comando)
        {
            try
            {
                string sp = "SP_SEL_ESQPLANTA";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_EsqPlanta> lista = new List<MOD_EsqPlanta>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_EsqPlanta obj = new MOD_EsqPlanta();
                    obj.ESQ_codesqplanta = Convert.ToInt32(dt.Rows[i]["ESQ_CODESQPLANTA"]);
                    obj.ESQ_nombreesqplanta = dt.Rows[i]["ESQ_NOMBREESQPLANTA"].ToString();

                    lista.Add(obj);
                }

                return lista;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }
        public MOD_EsqPlanta SEL_EsqPlantaByID(SqlCommand comando, int codigo)
        {
            try
            {
                string sp = "SP_SEL_EsqPlantaByID";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ESQ_CODESQPLANTA", SqlDbType.Int));

                comando.Parameters[0].Value = codigo;

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_EsqPlanta obj = new MOD_EsqPlanta();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    obj.ESQ_codesqplanta = Convert.ToInt32(dt.Rows[i]["ESQ_CODESQPLANTA"]);
                    obj.ESQ_nombreesqplanta = dt.Rows[i]["ESQ_NOMBREESQPLANTA"].ToString();
                }

                return obj;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public int SEL_EsqPlantaByNombre(SqlCommand comando, string  nombre)
        {
            try
            {
                string sp = "SP_SEL_EsqPlantaByNOmbre";
                int cant = 0;
                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ESQ_NOMBREESQPLANTA", SqlDbType.VarChar));

                comando.Parameters[0].Value = nombre;

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_EsqPlanta obj = new MOD_EsqPlanta();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cant = Convert.ToInt32(dt.Rows[i]["ESQPLANTA_CANT"]);
                }

                return cant;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }
    }


}
